package com.pm2.dao.plan;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.pm2.entity.plan.MasterPlan;

public interface MasterPlanDao  extends JpaRepository<MasterPlan, Integer> {
	
	public MasterPlan findById(int id);
	
	@Query("from MasterPlan where project_id= ?1 order by sort_number desc")
	public List<MasterPlan> findByProjectId(int project_id);
	
	@Query("select min(mp.plan_start_date) from MasterPlan as mp where project_id= ?1")
	public Date findMinStartDateByProjectId(int project_id);
	
	@Query("select max(mp.plan_end_date) from MasterPlan as mp where project_id= ?1")
	public Date findMaxEndDateByProjectId(int project_id);
	
	@Modifying
	@Transactional
	@Query("delete MasterPlan where id= ?1")
	int deleteById(int id);
	
	@Modifying
	@Transactional
	@Query("update MasterPlan masterPlan set masterPlan.sort_number = masterPlan.sort_number+1 where pid= ?1 and sort_number >= ?2")
	int updateSortNumberById(int pid,int sort_number);
}
